package cn.edu.jxau.core.util;

/**
 * 分页助手类
 * 必须设定每页量
 * 起始页默认为1
 * 设定总量的时候，自动配置了最后一页
 * MangoDai
 * 2017 07 18 1:09 PM
 */
public class PageHelp {
    //当前页 默认为1
    private int startPage;
    //最后一页
    private int endPage;
    //每页数量
    private int pageSize;
    //数据库分页
    private int start;
    //数据总量
    private int total;
    //分页起始页
    private int front;
    //分页尾页
    private int back;

    public static int size = 10;

    public int getFront() {
        return front;
    }

    public void setFront(int front) {
        this.front = front;
    }

    public int getBack() {
        return back;
    }

    public void setBack(int back) {
        this.back = back;
    }

    public PageHelp(int pageSize) {
        super();
        setStartPage(1);
        setPageSize(pageSize);
    }

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public void setStartPage(int startPage) {
        this.startPage = startPage;
    }

    public void setEndPage(int endPage) {
        this.endPage = endPage;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * 会通知配置最后一页。
     * 公式 总 / 每页量，如果由余数就+1
     * 并且按10页自动分页设置front和back
     *
     * @param total
     */
    public void setTotal(int total) {
        this.total = total;
        if (0 != total) {
            int remainder = this.total % this.pageSize;
            this.endPage = this.total / this.pageSize;
            if (remainder > 0) {
                this.endPage++;
            }
        }
        if ((this.startPage - 5) < 1) {
            front=1;
        }
        this.back = this.startPage + 4;
        if(this.back >= this.endPage) {
            this.back = this.endPage;
        }
        setStart((this.getStartPage() - 1) * this.pageSize);
    }

    public int getStartPage() {
        return startPage;
    }

    public int getEndPage() {
        return endPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public int getTotal() {
        return total;
    }
}
